# coding:utf-8
from rest_framework.views import exception_handler
from .response import APIResponse
from .logger import log


def common_exception_handler(exc, context):
    log.error('%s error：%s' % (context['view'].__class__.__name__, str(exc)))  # context['view']view对象
    ret = exception_handler(exc, context)  # ret是Response对象，内部有个data
    if not ret:  # drf未处理的异常
        # 这里写更细粒度的逻辑判断
        if isinstance(exc, KeyError):
            return APIResponse(code=0, msg='key error')
        return APIResponse(code=0, msg='error', result=str(exc))
    else:
        return APIResponse(code=0, msg='error', result=ret.data)  # ret.data为字典
